﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace ESpace
{
    [ToolboxBitmap(typeof(EButton), "Button.bmp")]
    public class EButton : Control
    {
        bool LastDownStatus = false;
        bool bFirst = true;

        Brush brFace;
        Brush brFaceAct;
        Pen pnShadow1;
        Pen pnShadow2;
        Brush brLight1;
        Brush brLight2;
        Pen pnBorderAct;
        Brush brText;

        #region *** Свойства *********************************************************

        bool bAct = false;
        bool pDown = false;
        bool pVertText = false;
        bool pDownStand = false;

        Image pImg = null;
        [DefaultValue(null)]
        public Image Img { get { return pImg; } set { pImg = value; DrawAll(); } }
        [DefaultValue(false)]
        public bool Down 
        { 
            get { return pDown; } 
            set 
            { 
                if (!value || (value && pDownStand)) 
                { 
                    pDown = value; 
                    DrawAll(); 
                } 
            } 
        }
        [DefaultValue(false)]
        public bool DownStand 
        {
            get { return pDownStand; }
            set
            {
                pDownStand = value;
                if (pDown && !pDownStand)
                {
                    pDown = false;
                    DrawAll();
                }
            }
        }
        [DefaultValue(false)]
        public bool VertText { get { return pVertText; } set { pVertText = value; DrawAll(); } }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                DrawAll();
            }
        }
        
        #endregion *** Свойства *********************************************************

        public EButton() :base()
        {
            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawAll();
            base.OnPaint(e);
        }

        void DrawAll()
        {
            if (bFirst)
            {
                bFirst = false;
                brFace = Brushes.Lavender;
                pnShadow1 = new Pen(Color.FromArgb(171, 172, 214));
                brLight1 = Brushes.White;
                pnShadow2 = Pens.White;
                brLight2 = new SolidBrush((Color.FromArgb(171, 172, 214)));
                brText = Brushes.Navy;
                pnBorderAct = new Pen(Color.FromArgb(235, 207, 245));
                brFaceAct = new SolidBrush(Color.FromArgb(239, 239, 252));
            }

            if (Enabled)
            {
                brFace = Brushes.Lavender;
                brText = Brushes.Navy;
            }
            else
            {
                brFace = new SolidBrush(Color.FromArgb(230, 230, 230));
                brText = Brushes.Silver; 
            }

            Graphics gr = Graphics.FromHwnd(Handle);

            gr.Clear(SystemColors.Control);
            Pen pnShadow;
            Brush brLight;
            if (pDown)
            {
                brLight = brLight2;
                pnShadow = pnShadow2;
            }
            else
            {
                brLight = brLight1;
                pnShadow = pnShadow1;
            }
            gr.FillRectangle(brLight, 0, 0, Width, Height);

            gr.DrawLine(pnShadow, 1, Height - 1, Width - 1, Height - 1);
            gr.DrawLine(pnShadow, 2, Height - 2, Width - 1, Height - 2);
            gr.DrawLine(pnShadow, 3, Height - 3, Width - 1, Height - 3);

            gr.DrawLine(pnShadow, Width - 1, 1, Width - 1, Height - 1);
            gr.DrawLine(pnShadow, Width - 2, 2, Width - 2, Height - 1);
            gr.DrawLine(pnShadow, Width - 3, 3, Width - 3, Height - 1);

            gr.FillRectangle(brFace, 2, 2, Width - 4, Height - 4);
            
            if (bAct && !pDown && Enabled)
            {
                gr.FillRectangle(brFaceAct, 2, 2, Width - 4, Height - 4);
            }

            SizeF sz = gr.MeasureString(Text, Font);
            int x = (int)(Width / 2 - sz.Width / 2);
            if (x < 4) x = 4;
            int y = (int)(Height / 2 - sz.Height / 2);

            if (pDown)
            {
                if (pImg != null)
                {
                    gr.DrawLine(pnShadow1, 0, 2, Width - 3, 2);
                    gr.DrawLine(pnShadow1, 2, 0, 2, Height - 3);
                    gr.DrawImage(pImg, 3, 3, Width - 4, Height - 4);
                }
                if (pVertText)
                {
                    gr.RotateTransform(-90);
                    float xv = (Height / 2 + sz.Width / 2) * -1;
                    if (xv < (4 - Height)) xv = 4 - Height;
                    float yv = Width / 2 - sz.Height / 2;
                    if (yv < 3) yv = 3;
                    gr.DrawString(Text, Font, brText, new RectangleF(xv-1, yv+1, Height - 7, sz.Height));
                    gr.RotateTransform(90);
                }
                else
                {
                    gr.DrawString(Text, Font, brText, new RectangleF(x + 1, y + 1, Width - 7, sz.Height));
                }
            }
            else
            {
                if (pImg != null)
                    gr.DrawImage(pImg, 2, 2, Width - 4, Height - 4);
                if (pVertText)
                {
                    gr.RotateTransform(-90);
                    float xv = (Height / 2 + sz.Width / 2) * -1;
                    if (xv < (4-Height)) xv = 4-Height;
                    float yv = Width / 2 - sz.Height / 2;
                    if (yv < 3) yv = 3;
                    gr.DrawString(Text, Font, brText, new RectangleF(xv, yv, Height - 7, sz.Height));
                    gr.RotateTransform(90);
                }
                else
                {
                    gr.DrawString(Text, Font, brText, new RectangleF(x, y, Width - 7, sz.Height));
                }
            }

            if (bAct && !pDown && Enabled)
            {
                gr.DrawRectangle(pnBorderAct, 2, 2, Width - 5, Height - 5);
            }

            gr.Dispose();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (Enabled) pDown = true;
            DrawAll();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (Enabled)
            {
                if (pDownStand)
                {
                    if (LastDownStatus == true)
                        pDown = false;
                }
                else
                {
                    pDown = false;
                }
            }
            LastDownStatus = pDown;
            DrawAll();
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (!bAct)
            {
                bAct = true;
                DrawAll();
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            bAct = false;
            DrawAll();
            base.OnMouseLeave(e);
        }

    }

}
